## Chapter 8 (Democratic Norms) 
## this was originally chapter 7, but got flipped with compromise late in the process 
## Winter 2023  

## create an indicator for party*wave 
data <- data %>% 
  mutate(party_year = case_when(
    dem == 1 & year == 2 ~ 1, 
    dem == 1 & year == 3 ~ 2, 
    dem == 1 & year == 4 ~ 3, 
    dem == 0 & year == 2 ~ 4, 
    dem == 0 & year == 3 ~ 5, 
    dem == 0 & year == 4 ~ 6))

##############################################
## Figure 8.1: Block Supreme Court Nominee  ##
## Data from the October 2020 Wave          ## 
##############################################

pdf(file="Figures/Druckman_etal_figure_8_1.pdf", 
    height = 11, 
    width = 8.5)
print(data %>% 
  filter(year == 3) %>% 
  ggplot(aes(apstable, blockcourt,
             group = rep)) + 
  geom_smooth(aes(color = as.factor(rep), 
                  fill = as.factor(rep)), se = T) + 
  scale_color_manual(name = "",
                      values = c("black","grey70"), 
                      labels = c("Democrat","Republican")) + 
  scale_fill_manual(name = "",
                    values = c("black","grey70"), 
                    labels = c("Democrat","Republican")) +  
  scale_x_continuous(breaks = ap_quantiles, 
                     labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
  scale_y_continuous(breaks = c(1:4),
                     limits = c(1,4),
                     labels = c("Strongly \n Oppose","Somewhat \n Oppose",
                                "Somewhat \n Support", "Strongly \n Support")) + 
  xlab("Animus")+
  ylab("Support for Blocking Judicial Nominees") + 
  #ylim(c(1,4)) + 
  #ggtitle("Same-Party Senators Should Block \n Out-Party President's Supreme Court Nominees") + 
  labs(color = "") + 
  theme_bw() + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "none", 
        panel.grid = element_blank()))   
dev.off() 

#############################################
## Figure 8.2: Support for Gerrymandering  ##
## October 2020 Wave                       ## 
############################################# 

pdf(file="Figures/Druckman_etal_figure_8_2.pdf", 
    height = 11, 
    width = 8.5)
print(data %>% 
  filter(partyid_w1 != 4 & partychange == 0 & year == 3) %>% 
  ggplot(aes(apstable, districtsown,
             group = dem)) + 
  geom_smooth(aes(color = as.factor(dem),
                  fill = as.factor(dem)), 
              method = "gam",
              se = T) + 
  scale_color_manual(values = c("grey70","black"), 
                     labels = c("Republicans","Democrats")) + 
  scale_fill_manual(values = c("grey70","black"), 
                    labels = c("Republicans","Democrats")) +  
  scale_x_continuous(breaks = ap_quantiles, 
                     labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
  scale_y_continuous(breaks = c(1:4),
                     limits = c(1,4),
                     labels = c("Strongly \n Oppose","Somewhat \n Oppose",
                                "Somewhat \n Support", "Strongly \n Support")) + 
  xlab("Animus")+
  ylab("Support for Gerrymander") + 
  #ylim(c(1,4)) + 
  #ggtitle("Support for Same-Party Gerrymander") + 
  labs(color = "") + 
  theme_bw() + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "none", 
        panel.grid = element_blank()))   
dev.off() 

#########################################
## Figure 8.3: Challenge Election Loss ## 
## October 2020 Wave                   ## 
######################################### 

## create an indicator for size of loss and party 
data <- data %>% 
  mutate(party_size = case_when(
    dem == 1 & conditionsize == 1 ~ 1, 
    dem == 1 & conditionsize == 2 ~ 2, 
    dem == 1 & conditionsize == 3 ~ 3, 
    dem == 0 & conditionsize == 1 ~ 4, 
    dem == 0 & conditionsize == 2 ~ 5, 
    dem == 0 & conditionsize == 3 ~ 6))


## As a faceted figure 
## factor variable for condition size 
data$size_factor <- factor(data$conditionsize,
                           labels = c ("1,000 Vote Margin",
                                       "10,000 Vote Margin",
                                       "100,000 Vote Margin")) 

pdf(file="Figures/Druckman_etal_figure_8_3.pdf", 
    height = 8.5, 
    width = 11)
print(data %>% 
        filter(year == 3 & !is.na(conditionsize)) %>% 
        ggplot(aes(apstable, challelection)) + 
        geom_smooth(color = "black",
                    method="gam",
                    se = T) + 
        scale_x_continuous(breaks = ap_quantiles, 
                           labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
        scale_y_continuous(breaks = c(1:4),
                           limits = c(1,4),
                           labels = c("Definitely Should \n Not Challenge",
                                      "Probably Should \n Not Challenge",
                                      "Probably Should \n Challenge",
                                      "Definitely Should \n Challenge")) + 
        xlab("Animus") +
        ylab("Support for Challenging the Election Outcome") + 
        facet_grid(cols = vars(size_factor)) + 
        #ylim(c(1,4.1)) + 
        #ggtitle("Should An In-Party Candidate Challenge an Election Loss") + 
        labs(lty = "") + 
        #guides(color=guide_legend(nrow=2,byrow=TRUE))+
        theme_bw() + 
        theme(plot.title = element_text(hjust=0.5),
              legend.position = "none", 
              panel.grid = element_blank())) 
dev.off() 

#############################################
## Figure 8.4: Change in Support for Norms ##
#############################################

## create an indicator for party*wave 
data <- data %>% 
  mutate(norm_year = case_when(
    dem == 1 & year == 1 ~ 1, 
    dem == 1 & year == 4 ~ 2, 
    dem == 0 & year == 1 ~ 3, 
    dem == 0 & year == 4 ~ 4))

## Rescale all items: run 0-4 instead of 1-5 
## also flip the scale of limits on executive power & gets things done 
data <- data %>% 
  mutate(temp_exec = fexecpower + 1, 
         temp_methods = fmethods + 1, 
         rs_exec = -1*temp_exec + 6, 
         rs_methods = -1*temp_methods + 6)  

## Facet these norm items 
## First step: data goes from wide to long 
## Second step: create a factor to label the norms 
## Third step: print! 
pdf(file = "Figures/Druckman_etal_figure_8_4.pdf",
    height = 11,
    width = 8.5) 
print(data %>%
  filter(year == 1 | year == 4) %>% 
  select(c(rs_exec, rs_methods, institution, checks, year, factor_year, apstable,dem)) %>% 
  pivot_longer(cols = c(rs_exec, rs_methods, institution, checks),
               names_to = "group",
               values_to = "eval") %>% 
  ## remove people who don't answer the norm question 
  filter(!is.na(eval)) %>%   
  ## make a variable labeling the norms
  mutate(norm_factor = factor(group,
                              levels = c("rs_exec","rs_methods","institution","checks"),
                              labels = c("Executive Power \n (Reverse Scaled)",
                                         "Gets Things Done \n (Reverse Scaled)",
                                         "Respect for Institutions",
                                         "Checks and Balances"))) %>%  
  ggplot(aes(apstable, eval, 
             group = dem)) + 
  geom_smooth(aes(color = as.factor(dem),
                  fill = as.factor(dem)),
                  se = T) + 
  scale_color_manual(values = c("grey70","black"), 
                     labels = c("Republicans","Democrats")) + 
  scale_fill_manual(values = c("grey70","black"), 
                    labels = c("Republicans","Democrats")) +  
  scale_x_continuous(breaks = ap_quantiles, 
                     labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
  scale_y_continuous(breaks = c(1:5),
                     limits = c(1,5),
                     labels = c("Strongly \n Disagree","Somewhat \n Disagree",
                                "Neither Agreee \n nor Disagree",
                                "Somewhat \n Agree", "Strongly \n Agree")) + 
  xlab("Animus")+
  ylab("Agreement") + 
  #ylim(c(1,4)) + 
  ggtitle("") + 
  labs(color = "") + 
  theme_bw() + 
  facet_grid(rows = vars(norm_factor), cols = vars(factor_year)) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "none", 
        panel.grid = element_blank()))   
dev.off() 

################################################
## Figure 8.5: Break the Law or Use Violence? ##
################################################ 

pdf(file = "Figures/Druckman_et_al_Figure_8_5.pdf",
    height = 8.5,
    width = 11)
print(data %>%
        filter(year == 3) %>% 
        select(c(dem,apstable,engageunlawful,engageviolence)) %>% 
        ## wide to long: actions post-election 
        pivot_longer(cols = c(engageunlawful,engageviolence),
                     names_to = "group",
                     values_to = "eval") %>%  
        ## remove people who don't answer the relevant qs
        filter(!is.na(eval) & !is.na(apstable)) %>%   
        ## make a variable labeling the actions & the variables 
        mutate(action_factor = factor(group,
                                      levels = c("engageunlawful","engageviolence"),
                                      labels = c("Break the Law",
                                                 "Engage in Violence"))) %>% 
        ggplot(aes(apstable, eval,
                   group = dem)) +  
        geom_smooth(aes(color = as.factor(dem),
                        fill = as.factor(dem)), 
                    method = "gam",
                    se = T) + 
        scale_color_manual(values = c("grey70","black"), 
                           labels = c("Republicans","Democrats")) + 
        scale_fill_manual(values = c("grey70","black"), 
                          labels = c("Republicans","Democrats")) +  
        scale_x_continuous(breaks = ap_quantiles, 
                             labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
        scale_y_continuous(breaks = c(1:4),
                           limits = c(1,4),
                           labels = c("Not at All \n Likely","Not Too \n Likely",
                                      "Somewhat \n Likely", "Very Likely")) + 
        xlab("Animus")+
        ylab("Likelihood of Taking Action") + 
        #ggtitle("Partisan Animus and Extra-Legal Action") + 
        theme_bw() + 
        facet_grid(cols = vars(action_factor)) + 
        theme(plot.title = element_text(hjust=0.5),
              legend.position = "none", 
              panel.grid = element_blank()))  
dev.off() 


## Table A8.1: Please see replication data for Kingzette et al. 

##########################################################################
## Table A8.2: Replication of Kingzette et al. (POQ) w/ April 2021 Data ##
##########################################################################  

# Creating other variables we should control for: 1) political knowledge; 2) religion as fixed effect
data <- data %>%
  # Political knowledge scale from 0 to 5 based on number of Qs answered correctly
  mutate(p_knowledge_veto = ifelse(is.na(p_knowledge_veto) == TRUE, 0, 
                                   p_knowledge_veto),
         p_knowledge_hofr = ifelse(is.na(p_knowledge_hofr) == TRUE, 0, 
                                   p_knowledge_hofr),
         p_knowledge_constitution = ifelse(is.na(p_knowledge_constitution) == TRUE, 
                                           0, p_knowledge_constitution),
         p_knowledge_vp = ifelse(is.na(p_knowledge_vp) == TRUE, 0,
                                 p_knowledge_vp),
         p_knowledge_conservative = ifelse(is.na(p_knowledge_conservative) == TRUE,
                                           0, p_knowledge_conservative),
         know1 = ifelse(p_knowledge_veto == 4, 1, 0),
         know2 = ifelse(p_knowledge_hofr == 1, 1, 0),
         know3 = ifelse(p_knowledge_constitution == 3, 1, 0),
         know4 = ifelse(p_knowledge_vp == 3, 1, 0),
         know5 = ifelse(p_knowledge_conservative == 2, 1, 0),
         knowledge = know1 + know2 + know3 + know4 + know5,
         
         # Indicator variables for religious groups
         protestant = if_else(religion == 1,1,0),
         catholic = if_else(religion == 2,1,0),
         jewish  = if_else(religion == 3,1,0),
         other = if_else(religion == 4 | religion == 5 | religion == 6,1,0),
         not_relig = if_else(religion == 7,1,0),
         
         # Indicator variable for non-Hispanic white
         white = ifelse(race == 1, 1, 0),
         
         # Indicator variable for female
         female = ifelse(gender == 2, 1, 
                         ifelse(gender == 1 |
                                  gender == 3 |
                                  gender == 4, 0, NA))) 

# Creating measure of ideology based on policy positions
policy_pca <- data %>%
  select(soc_sec_spending, healthcare, services_spending, gov_role,
         abortion, homosexual_protection, mmigration, trans_protection) %>%
  na.omit() %>%
  prcomp(scale. = TRUE,
         center = TRUE)
summary(policy_pca) # pretty indicative data is mostly on one dimension

# Adding policy liberalism measure to data set
data <- data %>%
  filter(!is.na(soc_sec_spending) & !is.na(healthcare) & !is.na(services_spending) & 
           !is.na(gov_role) & !is.na(abortion) & !is.na(homosexual_protection) &
           !is.na(mmigration) & !is.na(trans_protection)) %>%
  mutate(conservatism = scales::rescale(-policy_pca$x[,1])) 

## dummy for Republican 
data$republican <- ifelse(data$partyid_w1 > 4,1, 
                          ifelse(data$partyid_w1 < 4,0,NA)) 

# Running models paralleling Kingzette et al. 
methods_modideo <- lm(fmethods ~ republican + apstable + knowledge + conservatism +
                        education + white + female + protestant + 
                        catholic + jewish + not_relig + republican*apstable,
                      data = data, 
                      subset = year == 4) 
summary(methods_modideo)

checks_modideo <- lm(checks ~ republican + apstable + knowledge + conservatism +
                       education + white + female + protestant + 
                       catholic + jewish + not_relig + republican*apstable,
                     data = data, 
                     subset = year == 4) 
summary(checks_modideo)

exec_power_modideo <- lm(fexecpower ~ republican + apstable + knowledge + conservatism +
                           education + white + female + protestant + 
                           catholic + jewish + not_relig + republican*apstable,
                         data = data, 
                         subset = year == 4) 
summary(exec_power_modideo)

institu_respect_modideo <- lm(institution ~ republican + apstable + knowledge + conservatism +
                                education + white + female + protestant + 
                                catholic + jewish + not_relig + republican*apstable,
                              data = data, 
                              subset = year == 4) 
summary(institu_respect_modideo)

summary(lm(institution ~ republican + apstable+republican*apstable,
     data = data, 
     subset = year == 4)) 

stargazer(exec_power_modideo, methods_modideo, institu_respect_modideo, checks_modideo,
          type = "html",
          covariate.labels = c("Republican",
                               "Animus",
                               "Political Knowledge",
                               "Conservatism",
                               "Education",
                               "Non-Hispanic White",
                               "Female",
                               "Religion: Protestant",
                               "Religion: Catholic",
                               "Religion: Jewish",
                               "Religion: None",
                               "Rep.* Animus"),
          out = "Tables/raw/Table_A8_2.htm")

######################################
## Panel Models for the Norms Items ##
######################################

data$norm_year <- ifelse(data$year == 1,0,
                         ifelse(data$year == 4,1,NA)) 

m0 <- felm(checks ~ apstable*rep*norm_year | uniqueid + norm_year | 0,
           subset = partyid_w1 != 4 & partychange == 0, 
             data = data) 
m1 <- felm(institution ~ apstable*rep*norm_year | uniqueid + norm_year | 0,
           subset = partyid_w1 != 4 & partychange == 0, 
             data = data) 
m2 <- felm(fmethods ~ apstable*rep*norm_year | uniqueid + norm_year | 0,
           subset = partyid_w1 != 4 & partychange == 0, 
             data = data) 
m3 <- felm(fexecpower ~ apstable*rep*norm_year | uniqueid + norm_year | 0,
           subset = partyid_w1 != 4 & partychange == 0, 
             data = data) 


## output this to a table 
models <- list()
models[["Executive Power"]] <- m3
models[["Gets Things Done"]] <- m2
models[["Respect for Institutions"]] <- m1 
models[["Checks and Balances"]] <- m0  

## add the N 
rows <- tribble(~term,~"m0", ~"m1",~"m2",~"m3",
                'N',m0$N,m1$N,m2$N,m3$N)
attr(rows,'position') <- 15 

## clean up the GOF output (just N & R-Squared)
gm <- modelsummary::gof_map 
gm$omit <- TRUE 
gm$omit[5] <- FALSE 

msummary(models,
         add_rows = rows, 
         coef_map = c("apstable" = "Animus",
                      "rep" = "Republican",
                      "norm_year" = "April 2021 Wave",
                      "apstable:rep" = "Animus*Republican",
                      "apstable:norm_year" = "Animus*April 2021 Wave",
                      "rep:norm_year" = "Republican*April 2021 Wave", 
                      "apstable:rep:norm_year" = "Animus*Republican*April 2021 Wave"), 
         gof_map = gm, 
         stars = T, 
         output =  "Tables/raw/table_A8_3.html") 


